using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP.Commons
{
    public partial class frmExceptionMsg : Form
    {
        public frmExceptionMsg()
        {
            InitializeComponent();
        }

        private string m_strExceptionMessage = "";
        private string m_strModuleName = "";
        private string m_strMethodName = "";
        private string m_strFileName = "";
        private string m_strFileLineNumber = "";
        private string m_strFileColumnNumber = "";
        private string m_strDataTime = "";
        private DataTable m_dtStackInfo = null;
        //private string m_strBaseMethodName = "";
        //private string m_strBaseFileName = "";
        //private string m_strBaseFileLineNumber = "";


        public string ExceptionMessage
        {
            get
            {
                return m_strExceptionMessage;
            }
            set
            {
                m_strExceptionMessage = value;
            }
        }

        public string ModuleName
        {
            get
            {
                return m_strModuleName;
            }
            set
            {
                m_strModuleName = value;
            }
        }
        public string MethodName
        {
            get
            {
                return m_strMethodName;
            }
            set
            {
                m_strMethodName = value;
            }
        }

        public string FileName
        {
            get
            {
                return m_strFileName;
            }
            set
            {
                m_strFileName = value;
            }
        }

        public string FileLineNumber
        {
            get
            {
                return m_strFileLineNumber;
            }
            set
            {
                m_strFileLineNumber = value;
            }
        }

        public string FileColumnNumber
        {
            get
            {
                return m_strFileColumnNumber;
            }
            set
            {
                m_strFileColumnNumber = value;
            }
        }

        public string DataTime
        {
            get
            {
                return m_strDataTime;
            }
            set
            {
                m_strDataTime = value;
            }
        }

        public DataTable StackInfo
        {
            get
            {
                return m_dtStackInfo;
            }
            set
            {
                m_dtStackInfo = value;
            }
        }



        #region Private Events
        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmExceptionMsg_Load(object sender, System.EventArgs e)
        {

            if (m_strExceptionMessage == "") m_strExceptionMessage = SysVar.WarnTitle; 
            this.txtMessage.Text    = m_strExceptionMessage;
            this.txtModuleName.Text = m_strModuleName;
            this.txtMethodName.Text = m_strMethodName;
            this.txtFileName.Text   = m_strFileName;
            this.txtFileLineNumber.Text = m_strFileLineNumber;
            this.txtFileColumnNumber.Text = m_strFileColumnNumber;

            rbtYes.Checked = true;
            if (rbtYes.Checked)
                ShowDataGrid(true);
            else
                ShowDataGrid(false);
            btnDetail_Click(this, e);

        }

        private void dgException_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgException.CurrentCell != null)
            {
                if (!dgException.CurrentRow.Selected)
                    dgException.CurrentRow.Selected = true;
            }
        }

        private void dgException_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgException.CurrentCell != null)
            {
                if (!dgException.CurrentRow.Selected)
                    dgException.CurrentRow.Selected = true;

                string target = "";
                target = dgException[1, dgException.CurrentCell.RowIndex].Value.ToString();
                if (target != "")
                    System.Diagnostics.Process.Start(target);
            }
        }

        private void dgException_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            dgException.CurrentRow.Selected = true;
        }

        private void lnkBaseFile_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            //string target = m_strBaseFileName;
            //if (target != "")
            //{
            //    // Determine which link was clicked within the LinkLabel.
            //    this.lnkBaseFile.Links[lnkBaseFile.Links.IndexOf(e.Link)].Visited = true;
            //    System.Diagnostics.Process.Start(target);
            //}

        }

        private void lnkFile_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            string target = m_strFileName;
            if (target != "")
            {
                this.lnkFile.Links[lnkFile.Links.IndexOf(e.Link)].Visited = true;
                System.Diagnostics.Process.Start(target);
            }
        }

        private void rbtYes_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtYes.Checked)
                ShowDataGrid(true);
            else
                ShowDataGrid(false);
        }

        private void rbtNo_CheckedChanged(object sender, EventArgs e)
        {
            //if (rbtYes.Checked)
            //    ShowDataGrid(true);
            //else
            //    ShowDataGrid(false);
        }
        #endregion

        #region Private Functions
        private void ShowDataGrid(bool DisplaySystemFunc)
        {
            this.dgException.DataSource = null;

            if (m_dtStackInfo != null)
            {
                m_dtStackInfo.DefaultView.AllowEdit = false;
                if (DisplaySystemFunc)
                {
                    m_dtStackInfo.DefaultView.RowFilter = "";
                }
                else
                {
                    m_dtStackInfo.DefaultView.RowFilter = "IsUser = 'Yes'";
                }
                this.dgException.DataSource = m_dtStackInfo;
                this.dgException.AutoResizeColumns();
            }
        }
        #endregion

        private void btnDetail_Click(object sender, EventArgs e)
        {
            if (this.btnDetail.Text == "&<<Detail")
            {
                this.Height -= this.gpbException.Height;
                this.btnDetail.Text = "&Detail>>"; 
            }
            else
            {
                this.Height += this.gpbException.Height;
                this.btnDetail.Text = "&<<Detail"; 
            }
        }



    }
}